package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.job.AddJobRequest;
import zengjin.model.dto.job.PageJobRequest;
import zengjin.model.dto.job.UpdateJobRequest;
import zengjin.model.en.Job;
import zengjin.model.vo.job.JobAndEmployerVO;
import zengjin.model.vo.job.JobVO;
import zengjin.service.JobService;

/**
 * @author 19324
 */
@Tag(name = "岗位相关接口")
@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {

    @Resource
    private JobService jobService;

    //1.添加岗位
    @Operation(summary = "添加岗位")
    @PostMapping("/addJob")
    public R<Boolean> addJob(@RequestBody AddJobRequest addJobRequest) {
        //1.参数校验
        if (addJobRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean addJobResult= jobService.addJob(addJobRequest);
        if(!addJobResult){
            return R.error(false,"添加岗位失败");
        }
        return R.success(true,"添加岗位成功");
    }

    //2-1.分页查询岗位
    @Operation(summary = "分页查询岗位")
    @PostMapping("/pageJob")
    public R<PageResponse<JobVO>> pageJob(@RequestBody PageJobRequest pageJobRequest) {
        //1.参数校验
        if (pageJobRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<JobVO> pageJobResult = jobService.pageJob(pageJobRequest);
        return R.success(pageJobResult,"分页查询岗位成功");
    }

    //2-2.分页查询岗位和企业信息
    @Operation(summary = "分页查询岗位和企业信息")
    @PostMapping("/pageJobAndEmployer")
    public R<PageResponse<JobAndEmployerVO>> pageJobAndEmployer(@RequestBody PageJobRequest pageJobRequest) {
        //1.参数校验
        if (pageJobRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<JobAndEmployerVO> pageJobAndEmployerResult = jobService.pageJobAndEmployer(pageJobRequest);
        return R.success(pageJobAndEmployerResult,"分页查询岗位和企业信息成功");
    }



    //3-1.查询岗位详情
    @Operation(summary = "查询岗位详情")
    @PostMapping("/getJobDetailByJobId")
    public R<JobVO> getJobDetailByJobId(Long jobId) {
        //1.参数校验
        if (jobId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Job byId = jobService.getById(jobId);
        JobVO jobVO = BeanUtil.copyProperties(byId, JobVO.class);
        return R.success(jobVO,"查询岗位详情成功");
    }

    //3-2.根据id查询岗位详情和企业详情
    @Operation(summary = "根据id查询岗位详情和企业详情")
    @PostMapping("/getJobAndEmployerDetailByJobId")
    public R<JobAndEmployerVO> getJobAndEmployerDetailByJobId(Long jobId) {
        //1.参数校验
        if (jobId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        JobAndEmployerVO jobAndEmployerVO = jobService.getJobAndEmployerDetailByJobId(jobId);
        return R.success(jobAndEmployerVO,"查询岗位详情和企业详情成功");
    }

    //4.根据id修改岗位信息
    @Operation(summary = "根据id修改岗位信息")
    @PostMapping("/updateJobById")
    public R<Boolean> updateJobById(@RequestBody UpdateJobRequest updateJobRequest) {
        //1.参数校验
        if (updateJobRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean updateJobResult = jobService.updateJobById(updateJobRequest);
        if(!updateJobResult){
            return R.error(false,"修改岗位失败");
        }
        return R.success(true,"修改岗位成功");
    }

    //5.根据id删除岗位信息
    @Operation(summary = "根据id删除岗位信息")
    @PostMapping("/deleteJobById")
    public R<Boolean> deleteJobById(Long jobId) {
        //1.参数校验
        if (jobId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean deleteJobResult = jobService.removeById(jobId);
        if(!deleteJobResult){
            return R.error(false,"删除岗位失败");
        }
        return R.success(true,"删除岗位成功");
    }

    //6.管理员-修改岗位状态
    @Operation(summary = "管理员-修改岗位状态")
    @PostMapping("/updateJobAdminStatusById")
    public R<Boolean> updateJobAdminStatusById(Long jobId, String jobAdminStatus) {
        //1.参数校验
        if (jobId == null||jobAdminStatus == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",jobId);
        updateWrapper.set("job_admin_status",jobAdminStatus);
        boolean updateJobAdminStatusResult = jobService.update(updateWrapper);
        if(!updateJobAdminStatusResult){
            return R.error(false,"岗位"+jobAdminStatus+"失败");
        }
        return R.success(true,"岗位"+jobAdminStatus+"成功");
    }

    //7.管理员-分页获取岗位
    @Operation(summary = "管理员-分页获取岗位")
    @PostMapping("/getJobAdminPage")
    public R<PageResponse<JobVO>> getJobAdminPage(@RequestBody PageJobRequest pageJobRequest) {
        //1.参数校验
        if (pageJobRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<JobVO> jobAdminPage = jobService.getJobAdminPage(pageJobRequest);
        return R.success(jobAdminPage,"获取岗位成功");
    }


}
